const {
    exec
} = require('../../mysql')

// 查询所有菜品   get请求   无参数
const searchDishes = () => {
    let sql = `select * from dish order by isVaild desc;`
    return exec(sql)
}

// 按需查询所有菜品  post请求 参数：菜品类型dishType
const selectDishes = (dishType) => {
    let sql = `select * from dish where dishType = ${dishType} order by isVaild desc;`
    return exec(sql)
}


// 编辑菜品    post请求   dishId,dishName,originalPrice,presentPrice,isVaild,dishType,dishIntroduce
const editDish = (dishId, dishName, originalPrice, presentPrice, isVaild, dishType, dishIntroduce) => {
    let sql = `update dish set dishName = '${dishName}',originalPrice = ${originalPrice},presentPrice = ${presentPrice},
    isVaild = ${isVaild},dishType = ${dishType},dishIntroduce = '${dishIntroduce}' where dishId = ${dishId};
    `
    // console.log(sql)
    return exec(sql).then(Data => {
        if (Data.affectedRows > 0) {
            return true
        }
        return false
    })
}

// 添加菜品   post请求   dishName,imgUrl,originalPrice,presentPrice,isVaild,dishType,dishIntroduce
const uploadDish = (dishName, imgUrl, originalPrice, presentPrice, isVaild, dishType, dishIntroduce) => {
    let sql = `insert into dish (dishName,imgUrl,originalPrice,presentPrice,isVaild,dishType,dishIntroduce) values(
        '${dishName}','${imgUrl}',${originalPrice},${presentPrice},${isVaild},${dishType},'${dishIntroduce}'
        );
    `
    return exec(sql).then(insertData => {
        if (insertData.affectedRows > 0) {
            return true
        }
        return false
    })
}

module.exports = {
    searchDishes,
    editDish,
    uploadDish,
    selectDishes,
}